import { listUpdate, tableUpdate } from '../../../../../common/utils/table.js';
import { initExecute } from '../../../../../common/utils/index.js';
import { _SuperTable } from '../../../../../../config.js';
import open from '../../../../../common/utils/open.js';
const SuperTable = (hook, { JiessBetween, JiessTable, Loading, Card }) => {
	return {
		name: 'SuperTable',
		isJiess: true,
		setup({
			beforeCreate,
			created,
			beforeMounte,
			mounted,
			beforeUpdate,
			updated,
			// ===========
			filter,
			container,
			tableProps,
			// 列表容器的参数
			tableBoxParam = {},
			// 分页容器的参数
			pagerBoxParam = {},
			// 列表容器参数，常作用于card
			containerParam = {},
			// ===========
			...baseParam
		}) {
			const { $ref } = hook.$m;
			const boxParam = {
				// 列表左上标题
				title: '列表',
				// 默认带有列表card
				isCard: true,
				// 注入初始的全局配置
				..._SuperTable.boxParam
			};
			// 默认启用复选框
			const {
				// 列表表头配置
				items,
				// 初始化整体布局
				initLayout = _SuperTable.initLayout,
				// 列表接口
				httpApi,
				// 接口身份标识
				httpKey,
				// 列表唯一标记
				httpTag = '',
				// 是否立即加载列表
				immedicat = true,
				// 默认带分页的数据更新
				updateType = 'listUpdate',
				// 列表更新参数
				updateParam,
				// JiessTable的参数
				...other
			} = baseParam;
			// ==============================================================
			const extraParam = { ctx: this };
			// 组件跨层，将父级的插槽数据注入到内部组件
			const { $slots } = this.PJR._resConfig;
			// --------------------------------------------
			const initLoading = (midder) => {
				if (httpKey) {
					new Loading(httpKey + httpTag, [httpKey], boo => {
						if (midder.table) {
							if (midder.table.setLoading) {
								midder.table.setLoading(boo);
							} else {
								const timer = setTimeout(() => {
									clearTimeout(timer);
									midder.table.setLoading(boo);
								})
							}
						}
					})
				}
			}
			// --------------------------------------------
			let execFunc = null;
			// ==============================================================
			this.add({
				items,
				$slots,
				tableProps,
				is: JiessTable,
				create: async (midder) => {
					execFunc = initExecute.call(this, midder);
					execFunc(beforeCreate);
					Object.assign(boxParam, execFunc(container));
					midder.tableTAG = { title: boxParam.title, httpTag };
					midder.open = open.call(this, midder);
					await initLoading.call(this, midder);
					execFunc(created);
				},
				render(midder, { table, pager }) {
					const style = { marginTop: '8px' };
					// 列表容器头部节点
					const { isCard, title, extra, ...otherBoxParam } = boxParam;
					if (isCard) {
						containerParam.is = Card;
						if (title || extra) {
							containerParam.$slots = {
								// 插入card插槽的内容
								header: () => ({
									is: JiessBetween,
									// 构建左侧标题
									left: title,
									// 构建右侧工具
									right: extra,
									// 头部容器配置
									...otherBoxParam
								})
							};
						}
					}
					// 注入默认的分页容器样式，使分页和列表保持一定间隙
					if (!pagerBoxParam.style) pagerBoxParam.style = style;
					if (!tableBoxParam.style) tableBoxParam.style = style;
					const tableNode = Object.assign(containerParam, {
						children: [
							{ slot: 'tableRenderHeader' },
							table,
							{ slot: 'tableRenderFooter' },
							// 可通过$if控制是否显示分页
							[pagerBoxParam, pager]
						]
					});
					if (typeof initLayout === 'function') {
						this.add(initLayout.call(this, midder, tableNode));
					} else {
						this.add(execFunc(filter), tableBoxParam, tableNode);
					}
				},
				mounte: async (midder) => {
					execFunc(beforeMounte);
					// 初始化是否调用update更新列表
					if (immedicat) await midder.table.reload();
					execFunc(mounted);
				},
				update: async (midder) => {
					execFunc(beforeUpdate);
					const update = updateType === 'tableUpdate' ? tableUpdate : listUpdate;
					await update.call(this, httpApi, midder, updateParam);
					execFunc(updated);
				},
				...other
			});
		}
	}
};
export default SuperTable;